package com.levy.system;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.junit.Test;

import com.sun.management.UnixOperatingSystemMXBean;

public class SystemInformation {

	// ManagementFactory
	// Apache commons-io
//	@Test
	public void getRunTime() {
		RuntimeMXBean operatingSystemMXBean = ManagementFactory.getRuntimeMXBean();
		for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
			method.setAccessible(true);
			if (method.getName().startsWith("get") && Modifier.isPublic(method.getModifiers())) {
				Object value;
				try {
					value = method.invoke(operatingSystemMXBean);
				} catch (Exception e) {
					value = e;
				} // try
				System.out.println(method.getName() + " = " + value);
			} // if
		} // for
	}

	// ManagementFactory
	// Apache commons-io
	@Test
	public void getCPU2() {
		UnixOperatingSystemMXBean unixBean = null;
		OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
		System.out.println(operatingSystemMXBean.getArch());

		if (operatingSystemMXBean instanceof com.sun.management.UnixOperatingSystemMXBean) {
			System.out.println("UnixOperatingSystemMXBean");
			unixBean = (UnixOperatingSystemMXBean) operatingSystemMXBean;

		}

		long l = unixBean.getFreePhysicalMemorySize();
		System.out.println("get method" + l);
		System.out.println("get method" + unixBean.getSystemLoadAverage());

		for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
			method.setAccessible(true);
			if (method.getName().startsWith("get") && Modifier.isPublic(method.getModifiers())) {
				Object value;
				try {
					value = method.invoke(operatingSystemMXBean);
				} catch (Exception e) {
					value = e;
				} // try
				System.out.println(method.getName() + " = " + value);
			} // if
		} // for

		System.out.println(System.getProperty("os.arch"));
	}

}
